﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.DBModule;
using ZX.BLL.Extend;
using ZX.CRM.Bussiness;
using ZX.SYS.Library;
using ZX.CRM.Library;
using ZX.BLL;
using ZX.Common;
using ZX.Web.UI.Controls.ControlBind; 

namespace ZX.WebApp.Crm.Customer
{
    public partial class AddCustomerInfo : EditUIPage
    {
        BTCrmCustomer bll = new BTCrmCustomer();
        string strLinkMan = "", strLinkManRecord = "", strLinkManChoice = "",
               strAttach = "", strOrder = "", strPay = "", strTeachSump = "",
               strCustomerRecord = "", strRelationInfo = "", strCustomerInfo = "";
        BDatPhraseDetail bllDetail = new BDatPhraseDetail();
        protected void Page_Load(object sender, EventArgs e)
        {
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string ids = this.GetStringFromQueryString("id", "");
            if (DataAction == "update")
            {
                ids = "00000000-0000-0000-0000-000000000000";
            }
            BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            BaseEntity entity = bInstance.GetEntityById(ids) as BaseEntity;
            this.EditTemplate.Text = CreateInstanceJsObject(instance, entity);

            //if (!this.IsPostBack)
            {
                #region-------dropdownlist-------
                //省份
                IList<SysProvince> ListProvince = bll.GetAllProvince();
                DropDownListControl.DropDataSourceBind(this.Province, ListProvince, "Name", "Code");
                //客户规模(员工人数)
                IList<DatPhraseDetail> ListDetailNumber = bllDetail.GetDatPhraseDetailByClassValue("CustomerScope");
                DropDownListControl.DropDataSourceBind(this.PersonId, ListDetailNumber, "PhraseText", "PhraseID");
                //行业
                //IList<DatPhraseDetail> ListDetailTrade = bllDetail.GetDatPhraseDetailByClassValue("Trade");
                //DropDownListControl.DropDataSourceBind(this.TradeId, ListDetailTrade, "PhraseText", "PhraseID");
                //客户类型
                IList<DatPhraseDetail> ListDetailType = bllDetail.GetDatPhraseDetailByClassValue("CustomerType");
                DropDownListControl.DropDataSourceBind(this.TypeId, ListDetailType, "PhraseText", "PhraseID");
                //产值
                IList<DatPhraseDetail> ListDetailProduct = bllDetail.GetDatPhraseDetailByClassValue("Product");
                DropDownListControl.DropDataSourceBind(this.ProductId, ListDetailProduct, "PhraseText", "PhraseID");
                //客户来源
                IList<DatPhraseDetail> ListDetailOrigin = bllDetail.GetDatPhraseDetailByClassValue("CustomerSource");
                DropDownListControl.DropDataSourceBind(this.OriginId, ListDetailOrigin, "PhraseText", "PhraseID");
                //公司性质
                IList<DatPhraseDetail> ListDetailProperty = bllDetail.GetDatPhraseDetailByClassValue("Property");
                DropDownListControl.DropDataSourceBind(this.PropertyId, ListDetailProperty, "PhraseText", "PhraseID");
                //决策类型
                IList<DatPhraseDetail> ListDecisionType = bllDetail.GetDatPhraseDetailByClassValue("DecisionType");
                DropDownListControl.DropDataSourceBind(this.DecisionType, ListDecisionType, "PhraseText", "PhraseID");
                //购买类型
                IList<DatPhraseDetail> ListDecisionBuyType = bllDetail.GetDatPhraseDetailByClassValue("DecisionBuyType");
                DropDownListControl.DropDataSourceBind(this.DecisionBuyType, ListDecisionBuyType, "PhraseText", "PhraseID");
                //感觉类型
                IList<DatPhraseDetail> ListDecisionFeelType = bllDetail.GetDatPhraseDetailByClassValue("DecisionFeelType");
                DropDownListControl.DropDataSourceBind(this.DecisionFeelType, ListDecisionFeelType, "PhraseText", "PhraseID");
                //企业销售类型
                IList<DatPhraseDetail> ListCustomerSaleTyte = bllDetail.GetDatPhraseDetailByClassValue("CustomerSaleTyte");
                DropDownListControl.DropDataSourceBind(this.CustomerSaleTyte, ListCustomerSaleTyte, "PhraseText", "PhraseID");
                //培训态度
                IList<DatPhraseDetail> ListDecisionAttitude = bllDetail.GetDatPhraseDetailByClassValue("DecisionAttitude");
                DropDownListControl.DropDataSourceBind(this.DecisionAttitude, ListDecisionAttitude, "PhraseText", "PhraseValue");
                //客服关注等级
                IList<DatPhraseDetail> ListCompanyAttention = bllDetail.GetDatPhraseDetailByClassValue("CompanyAttention");
                DropDownListControl.DropDataSourceBind(this.CompanyAttention, ListCompanyAttention, "PhraseText", "PhraseValue");
                #endregion

                #region-------
                //新增信息
                if (DataAction == "insert")
                {
                    this.ID.Value = Guid.NewGuid().ToString();
                    this.Code.Value = IDMaker.GetSequenceStr("CUS001");
                    if (Flag != "3")
                    {
                        this.CustomeerMgrs.Value = this.UserId.ToString(); //客户经理ID
                        this.txtCustomeerMgrs.Value = this.User.UName.ToString();//客户经理名
                    }
                }
                else  //编辑信息
                {
                    string id = this.GetStringFromQueryString("id", "");
                    if (id != null)
                    {
                        IList<V_CrmCustomerInfo> list = bll.GetAllCustomerIn(new Guid(id));
                        if (list != null && list.Count > 0)
                        {
                            CustomerInfoDataBind(list);

                            #region ---------- jsonData ----------
                            //联系人
                            IList<V_CrmManager> listLinkMan = bll.GetLinkManagertInfoByCustomerId(new Guid(id));
                            if (listLinkMan != null && listLinkMan.Count > 0)
                            {
                                IList<BaseEntity> _listLinkMan = new List<BaseEntity>();
                                foreach (var _e in listLinkMan)
                                {
                                    _listLinkMan.Add(_e);
                                }
                                strLinkMan = _listLinkMan.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                                //ZX.Common.Globals.ObjectToJSON(listLinkMan);
                            }
                            //行销中心记录
                            IList<V_CrmActivityTel> listLinkRecord = bll.GetLinkRecordInfoByCustomerId(new Guid(id));
                            if (listLinkRecord != null && listLinkRecord.Count > 0)
                            {
                                IList<BaseEntity> _listLinkRecord = new List<BaseEntity>();
                                foreach (var _e in listLinkRecord)
                                {
                                    _listLinkRecord.Add(_e);
                                }
                                strLinkManRecord = _listLinkRecord.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                                //ZX.Common.Globals.ObjectToJSON(listLinkRecord);
                            }
                            //工作记录
                            IList<V_CrmActivityCall> listLinkManChoice = bll.GetLinkChoiceInfoByCustomerId(new Guid(id));
                            if (listLinkManChoice != null && listLinkManChoice.Count > 0)
                            {
                                IList<BaseEntity> _listLinkManChoice = new List<BaseEntity>();
                                foreach (var _e in listLinkManChoice)
                                {
                                    _listLinkManChoice.Add(_e);
                                }
                                strLinkManChoice = _listLinkManChoice.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                                //ZX.Common.Globals.ObjectToJSON(listLinkRecord);
                            }
                            //附件
                            IList<V_CrmCustomerAttach> listAttach = bll.GetAllCrmCustomerAttachInfo(new Guid(id));
                            if (listAttach != null && listAttach.Count > 0)
                            {
                                IList<BaseEntity> _listAttach = new List<BaseEntity>();
                                foreach (var _e in listAttach)
                                {
                                    _listAttach.Add(_e);
                                }
                                strAttach = _listAttach.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                                //ZX.Common.Globals.ObjectToJSON(listAttach);
                            }
                            else
                            {
                                IList<V_CrmCustomerAttach> listAttachInfo = bll.GetAllCrmCustomerInfoAttachInfo(new Guid(id));
                                if (listAttachInfo != null && listAttachInfo.Count > 0)
                                {
                                    IList<BaseEntity> _listAttachInfo = new List<BaseEntity>();
                                    foreach (var _e in listAttachInfo)
                                    {
                                        _listAttachInfo.Add(_e);
                                    }
                                    strAttach = _listAttachInfo.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                                }
                            }
                            //订单
                            IList<V_CrmCustomerInfoOrder> listCustomerOrder = bll.GetAllOrderInfos(new Guid(id));
                            if (listCustomerOrder != null && listCustomerOrder.Count > 0)
                            {
                                IList<BaseEntity> _listCustomerOrder = new List<BaseEntity>();
                                foreach (var _e in listCustomerOrder)
                                {
                                    _listCustomerOrder.Add(_e);
                                }
                                strOrder = _listCustomerOrder.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                            }
                            //缴款单
                            IList<V_CrmPay> listCusoterPay = bll.GetAllPayInfo(new Guid(id));
                            if (listCusoterPay != null && listCusoterPay.Count > 0)
                            {
                                IList<BaseEntity> _listCusoterPay = new List<BaseEntity>();
                                foreach (var _e in listCusoterPay)
                                {
                                    _listCusoterPay.Add(_e);
                                }
                                strPay = _listCusoterPay.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                            }
                            //讲师总结
                            IList<V_CrmTeachSumup> listTeachSumup = bll.GetAllTeachSumup(new Guid(id));
                            if (listTeachSumup != null && listTeachSumup.Count > 0)
                            {
                                IList<BaseEntity> _listTeachSumup = new List<BaseEntity>();
                                foreach (var _e in listTeachSumup)
                                {
                                    _listTeachSumup.Add(_e);
                                }
                                strTeachSump = _listTeachSumup.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                            }
                            //客户记录
                            IList<V_CrmCustomerRecord> listCustomerRecord = bll.GetCrmCustomerRecord(new Guid(id));
                            if (listCustomerRecord != null && listCustomerRecord.Count > 0)
                            {
                                IList<BaseEntity> _listCustomerRecord = new List<BaseEntity>();
                                foreach (var _e in listCustomerRecord)
                                {
                                    _listCustomerRecord.Add(_e);
                                }
                                strCustomerRecord = _listCustomerRecord.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                            }
                            //客户关系列表
                            IList<V_CrmCustomerRelationInfo> listRelationInfo = bll.GetCrmCustomerRelationInfoByCustomerId(new Guid(id));
                            if (listRelationInfo != null && listRelationInfo.Count > 0)
                            {
                                IList<BaseEntity> _listlistRelationInfo = new List<BaseEntity>();
                                foreach (var _e in listRelationInfo)
                                {
                                    _listlistRelationInfo.Add(_e);
                                }
                                strRelationInfo = _listlistRelationInfo.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                            }
                            //客户信息数据
                            IList<CrmCustomerInfo> listCrmCustomerInfo = bll.GetCrmCustomerInfo(new Guid(id));
                            if (listCrmCustomerInfo != null && listCrmCustomerInfo.Count > 0)
                            {
                                IList<BaseEntity> _listCrmCustomerInfo = new List<BaseEntity>();
                                foreach (var _e in listCrmCustomerInfo)
                                {
                                    _listCrmCustomerInfo.Add(_e);
                                }
                                strCustomerInfo = _listCrmCustomerInfo.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                            }
                            #endregion
                        }
                    }
                }
                #endregion
            }
        }

        private void CustomerInfoDataBind(IList<V_CrmCustomerInfo> list)
        {
            this.ID.Value = list[0].ID.ToString();
            this.Code.Value = list[0].Code.ToString();
            this.AddUserName.Value = list[0].AddUser.ToString();
            this.txtAddUserName.Value = list[0].AddUserName.ToString(); //信息专员
            this.SaleadviserId.Value = list[0].SaleadviserId.ToString(); //行销顾问
            this.txtSaleadviserId.Value = list[0].SaleadviserName.ToString();
            this.LectorName.Value = list[0].LectorUser.ToString(); //推广讲师
            this.txtLectorName.Value = list[0].LectorName.ToString();
            //this.Lectortime.Value = list[0].Lectortime.ToString("yyyy-MM-dd"); //讲场时间
            this.Lectortime.Value = list[0].HmDate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : list[0].HmDate.ToString("yyyy-MM-dd"); //讲场时间
            this.CustomeerMgrs.Value = list[0].CustomeerMgr.ToString(); //客户经理
            this.txtCustomeerMgrs.Value = list[0].CustomerMgrName.ToString();
            this.CustomerName.Value = list[0].CustomerName.ToString();
            this.CustomeerMgrs.Value = list[0].CustomeerMgr.ToString();

            this.ModifyUser.Value = this.UserId.ToString();
            this.ModifyTime.Value = DateTime.Now.ToString();

            IList<V_SysProvince> ListProvince = bll.GetProvinceInfo(list[0].Province);
            if (ListProvince != null && ListProvince.Count > 0)
            {
                this.Province.SelectedIndex = this.Province.Items.IndexOf(this.Province.Items.FindByText(ListProvince[0].Province));
            }
            IList<V_SysProvince> ListCity = bll.GetProvinceInfo(list[0].Province, list[0].City);
            if (ListCity != null && ListCity.Count > 0)
            {
                //绑定城市
                IList<SysCity> city = bll.GetAllCityByProviceName(list[0].Province);
                DropDownListControl.DropDataSourceBind(this.City, city, "Name", "Code");
                this.City.SelectedIndex = this.City.Items.IndexOf(this.City.Items.FindByText(ListCity[0].City));
            }
            IList<V_SysProvince> ListArea = bll.GetProvinceInfo(list[0].Province, list[0].City);
            if (ListArea != null && ListArea.Count > 0)
            {
                //绑定区县
                this.Area.DataSource = ListArea;
                this.Area.DataTextField = "Area";
                this.Area.DataValueField = "AreaCode";
                this.Area.DataBind();

                this.Area.SelectedIndex = this.Area.Items.IndexOf(this.Area.Items.FindByText(list[0].Area));
            }
            this.Address.Value = list[0].Address.ToString();
            this.Decisionmaker.Value = list[0].Decisionmaker.ToString();
            this.DecisionPosition.Value = list[0].DecisionPosition.ToString();
            this.DecisionTel.Value = list[0].DecisionTel.ToString();
            this.DecisionTel2.Value = list[0].DecisionTel2.ToString();
            this.Fax.Value = list[0].Fax.ToString();
            this.Tel1.Value = list[0].Tel1.ToString();
            this.Tel2.Value = list[0].Tel2.ToString();
            this.DecisionEmail.Value = list[0].DecisionEmail.ToString();
            this.Email.Value = list[0].Email.ToString();
            this.CompanyNetting.Value = list[0].CompanyNetting.ToString();
            this.exp2.Value = list[0].exp2;//年
            this.exp3.Value = list[0].exp3;//月
            this.exp4.Value = list[0].exp4;//日
            this.Post.Value = list[0].Post.ToString();
            this.MnemonicCode.Value = list[0].MnemonicCode.ToString();
            this.CompanyProduct.Value = list[0].CompanyProduct.ToString();
            this.CompanyGain.Value = list[0].CompanyGain.ToString();
            this.PersonSum.Value = list[0].PersonSum.ToString();
            this.PersonSum2.Value = list[0].PersonSum2.ToString();
            this.PersonSum3.Value = list[0].PersonSum3.ToString();
            this.ProductSum.Value = list[0].ProductSum.ToString();
            this.ProductSum2.Value = list[0].ProductSum3.ToString();
            this.ProductSum3.Value = list[0].ProductSum3.ToString();
            this.ProfitNum1.Value = list[0].ProfitNum1.ToString();
            this.ProfitNum2.Value = list[0].ProfitNum2.ToString();
            this.ProfitNum3.Value = list[0].ProfitNum3.ToString();
            this.CusTestimony.Value = list[0].CusTestimony.ToString();
            this.CusTrainbudget.Value = list[0].CusTrainbudget.ToString();
            this.DecisionBirthday.Value = list[0].DecisionBirthday.ToString("yyyy-MM-dd") == "1900-01-01" ? "" : list[0].DecisionBirthday.ToString("yyyy-MM-dd");
            this.Character.Value = list[0].Character.ToString();

            this.Homeplace.Value = list[0].Homeplace.ToString();
            this.SchoolScientifc.Value = list[0].SchoolScientifc.ToString();
            this.DecisionFancy.Value = list[0].DecisionFancy.ToString();
            this.DecisionTick.Value = list[0].DecisionTick.ToString();
            this.DecisionRest.Value = list[0].DecisionRest.ToString();
            this.DecisionLearn.Value = list[0].DecisionLearn.ToString();
            this.Credit.Value = list[0].Credit.ToString();
            this.PayOffNum1.Value = list[0].PayOffNum1.ToString();
            this.PayOffNum2.Value = list[0].PayOffNum2.ToString();
            this.PayOffNum3.Value = list[0].PayOffNum3.ToString();
            this.CompeterCustomer.Value = list[0].CompeterCustomer.ToString();
            this.TradeDegree.Value = list[0].TradeDegree.ToString();
            this.CustomerDream.Value = list[0].CustomerDream.ToString();
            this.DecisionPersonsun.Value = list[0].DecisionPersonsun.ToString();
            this.HighPersonsum.Value = list[0].HighPersonsum.ToString();
            this.MiddlePersonsum.Value = list[0].MiddlePersonsum.ToString();
            this.BelowPersonsum.Value = list[0].BelowPersonsum.ToString();
            this.TrainCompany.Value = list[0].TrainCompany.ToString();
            this.UnionId.Value = list[0].UnionId.ToString();
            this.AreaId.Value = list[0].AreaId.ToString();
            this.LevelId.Value = list[0].LevelId.ToString();
            this.TrainDate.Value = list[0].TrainDate.ToString("yyyy-MM-dd") == "1900-01-01" ? "" : list[0].TrainDate.ToString("yyyy-MM-dd");
            this.CustomerPromis.Value = list[0].CustomerPromis.ToString();
            this.AffectDecisioner.Value = list[0].AffectDecisioner.ToString();
            this.TrainHistory.Value = list[0].TrainHistory.ToString();
            this.Painful.Value = list[0].Painful.ToString();
            this.Pullulation.Value = list[0].Pullulation.ToString();
            this.CustomerPhylogeny.Value = list[0].CustomerPhylogeny.ToString();
            this.ManagerQuestion.Value = list[0].ManagerQuestion.ToString();
            this.StrongPoint.Value = list[0].StrongPoint.ToString();
            this.Explain.Value = list[0].Explain.ToString();
            this.AddTime.Value = list[0].AddTime.ToString();
            this.TradeId.Value = list[0].TradeId.ToString();
            this.txtTradeId.Value = list[0].TradeName;

            DropDownListControl.DropDownListSelect(this.DecisionSex, list[0].DecisionSex.ToString());
            DropDownListControl.DropDownListSelect(this.BirthdayType, list[0].BirthdayType.ToString());
            DropDownListControl.DropDownListSelect(this.IsTrainDept, list[0].IsTrainDept.ToString());
            DropDownListControl.DropDownListSelect(this.DecisionBuyType, list[0].DecisionBuyType.ToString());
            DropDownListControl.DropDownListSelect(this.DecisionType, list[0].DecisionType.ToString());
            DropDownListControl.DropDownListSelect(this.DecisionFeelType, list[0].DecisionFeelType.ToString());
            DropDownListControl.DropDownListSelect(this.CustomerSaleTyte, list[0].CustomerSaleTyte.ToString());
            //DropDownListControl.DropDownListSelect(this.TradeId, list[0].TradeId.ToString());
            DropDownListControl.DropDownListSelect(this.TypeId, list[0].TypeId.ToString());
            DropDownListControl.DropDownListSelect(this.ProductId, list[0].ProductId.ToString());
            DropDownListControl.DropDownListSelect(this.OriginId, list[0].OriginId.ToString());
            DropDownListControl.DropDownListSelect(this.PropertyId, list[0].PropertyId.ToString());
            DropDownListControl.DropDownListSelect(this.PersonId, list[0].PersonId.ToString());
            DropDownListControl.DropDownListSelect(this.DecisionAttitude, list[0].DecisionAttitude.ToString());
            DropDownListControl.DropDownListSelect(this.CompanyAttention, list[0].CompanyAttention.ToString());
        }

        #region----
        /// <summary>
        /// 添加人ID
        /// </summary>
        public string UserID
        {
            get { return this.UserId.ToString(); }
        }
        /// <summary>
        /// 页面标题
        /// </summary>
        public string PageTitle
        {
            get { return this.GetStringFromQueryString("title", ""); }
        }
        /// <summary>
        /// 状态
        /// </summary>
        public string Flag
        {
            get { return this.GetStringFromQueryString("flag", ""); }
        }
        /// <summary>
        /// 获取类型 (0:顾问 1:经理)
        /// </summary>
        public string Type
        {
            get { return this.GetStringFromQueryString("type", ""); }
        }
        /// <summary>
        /// 联系人信息
        /// </summary>
        public string LinkMan
        {
            get { return strLinkMan; }
        }
        /// <summary>
        /// 行销中心记录
        /// </summary>
        public string LinkRecord
        {
            get { return strLinkManRecord; }
        }
        /// <summary>
        /// 商机任务记录
        /// </summary>
        public string LinkChoice
        {
            get { return strLinkManChoice; }
        }
        /// <summary>
        /// 附件信息
        /// </summary>
        public string Attach
        {
            get { return strAttach; }
        }
        /// <summary>
        /// 订单信息
        /// </summary>
        public string Order
        {
            get { return strOrder; }
        }
        /// <summary>
        /// 缴款单信息
        /// </summary>
        public string Pay
        {
            get { return strPay; }
        }
        /// <summary>
        /// 讲师总结
        /// </summary>
        public string TeachSump
        {
            get { return strTeachSump; }
        }
        /// <summary>
        /// 分配记录
        /// </summary>
        public string CustomerRecord
        {
            get { return strCustomerRecord; }
        }
        /// <summary>
        /// 客户关系列表
        /// </summary>
        public string RelationInfo
        {
            get { return strRelationInfo; }
        }
        /// <summary>
        /// 客户信息数据
        /// </summary>
        public string CustomerInfo
        {
            get { return strCustomerInfo; }
        }
        #endregion
    }
}